package cn.briarbear.myblog.controller.admin;

import cn.briarbear.myblog.service.IAdminService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/adminlogin")
public class AdminLoginController {

    private final static Logger logger = LoggerFactory.getLogger(AdminLoginController.class);
    @Autowired
    private IAdminService adminService;
    /**
     * 后台登录跳转
     * @return
     */
    @GetMapping({"","/index","/login"})
    public String adminLogin(){
        logger.info("拦截到/adminlogin /adminlogin/index请求");
        return "admin/userlogin";
    }



    /**
     * 注销登录
     * @return
     */
    @GetMapping("/login_out")
    public String adminLoginOut(HttpServletRequest request, HttpServletResponse response){
        Authentication auth  = SecurityContextHolder.getContext().getAuthentication();
        if (auth != null)
            new SecurityContextLogoutHandler().logout(request,response,auth);

        adminService.destroySession(request);

        return "redirect:/";
    }
}
